1 using UnityEngine;
2 using
System.Collections;
3
4 ///
<summary>
5 ///
This class helps you to synchronize the velocities of a 2d physics RigidBody.
6 ///
Note that only the velocities are synchronized and because Unitys physics
7 ///
engine is not deterministic (ie. the results aren't always the same on all
8 ///
computers) - the actual positions of the objects may go out of sync. If you
9 ///
want to have the position of this object the same on all clients, you should
10 ///
also add a PhotonTransformView to synchronize the position.
11 ///
Simply add the component to your GameObject and make sure that
12 ///
the PhotonRigidbody2DView is added to the list of observed components
13 ///
</summary>
14 [RequireComponent(
typeof( PhotonView ) )]
15 [RequireComponent(
typeof( Rigidbody2D ) )]
16 [AddComponentMenu(
"Photon Networking/Photon Rigidbody 2D View")]
17 public
class PhotonRigidbody2DView : MonoBehaviour
18 {
19     
[SerializeField]
20     
bool m_SynchronizeVelocity = true;
21
22     
[SerializeField]
23     
bool m_SynchronizeAngularVelocity = true;
24
25     Rigidbody2D m_Body;
26
27     
void Awake()
28     {
29         m_Body = GetComponent<Rigidbody2D>();
30     }
31
32     
void OnPhotonSerializeView( PhotonStream stream, PhotonMessageInfo info )
33     {
34         
if( stream.isWriting == true )
35         {
36             
if( m_SynchronizeVelocity == true )
37             {
38                 stream.SendNext( m_Body.velocity );
39             }
40
41             
if( m_SynchronizeAngularVelocity == true )
42             {
43                 stream.SendNext( m_Body.angularVelocity );
44             }
45         }
46         
else
47         {
48             
if( m_SynchronizeVelocity == true )
49             {
50                 m_Body.velocity = (Vector2)stream.ReceiveNext();
51             }
52
53             
if( m_SynchronizeAngularVelocity == true )
54             {
55                 m_Body.angularVelocity = (
float)stream.ReceiveNext();
56             }
57         }
58     }
59 }


This class helps you to synchronize the velocities of a 2d physics RigidBody.

Note that only the velocities are synchronized and because Unitys physics

engine is not deterministic (ie. the results aren't always the same on all

computers) - the actual positions of the objects may go out of sync. If you

want to have the position of this object the same on all clients, you should

also add a PhotonTransformView to synchronize the position.

Simply add the component to your GameObject and make sure that

the PhotonRigidbody2DView is added to the list of observed components




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.565 lượt xem

Gõ tìm kiếm nhanh...